<!--小区列表页面 -->
<template>
  <div>
    <!-- 面包屑  -->
    <div id="div1">
      <!-- :显示当前路劲,快速返回之前的页面 -->
      <div id="navigation">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/admin' }"
            >资产管理</el-breadcrumb-item
          >
          <el-breadcrumb-item>资产设备列表</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 刷新 :size="mini" -->
      <div id="push">
        <i class="el-icon-refresh" @click="push"></i>
      </div>
    </div>
    <hr />
    <div>
      <!-- 搜索框部分 -->
      <div id="serch">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item label="日期">
            <el-date-picker
              v-model="startTime"
              type="datetime"
              placeholder="选择开始时间"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
            <el-date-picker
              v-model="endTime"
              type="datetime"
              placeholder="选择结束时间"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="设备名称">
            <el-input v-model="equName" placeholder="请输入设备名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              :plain="true"
              icon="el-icon-search"
              @click="onSubmit"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <!-- 按钮,记录条数 -->
      <div id="div3" style="width: 100%">
        <!-- 删除添加按钮 -->
        <div id="butt1">
          <el-button type="danger" @click="deleteChoose">批量删除</el-button>
          <el-button type="primary" @click="addA">添加</el-button>
        </div>
        <!-- 显示记录条数 -->
        <div id="butt2">
          <h3>
            共有数据:<el-link type="danger">{{ tableData.length }}</el-link
            >条
          </h3>
        </div>
      </div>
      <!-- 主体查询部分 -->
      <div id="tableg">
        <el-table
          :data="
            tableData.slice(
              (currentPage - 1) * pagesize,
              currentPage * pagesize
            )
          "
          @selection-change="handleSelectionChange"
          :header-cell-style="{
            background: '#eef1f6',
            color: '#606266',
            textAlign: 'center',
          }"
          :cell-style="{ textAlign: 'center' }"
          style="width: 100%"
        >
          <el-table-column
            type="selection"
            width="65px"
            style="border-right: 1px solid gray"
          >
          </el-table-column>
          <!-- id -->
          <el-table-column label="ID" width="65px">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <!-- 小区名称 -->
          <el-table-column label="小区名称">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{
                scope.row.community.comName
              }}</span>
            </template>
          </el-table-column>
          <!-- 设备编号 -->
          <el-table-column label="设备编号">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.equId }}</span>
            </template>
          </el-table-column>
          <!-- 设备名称 -->
          <el-table-column label="设备名称">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.equName }}</span>
            </template>
          </el-table-column>

          <!-- 品牌 -->
          <el-table-column label="品牌">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.brand }}</span>
            </template>
          </el-table-column>
          <!-- 购买单价 -->
          <el-table-column label="购买单价(￥)">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.equPrice }}</span>
            </template>
          </el-table-column>
          <!-- 购买数量 -->
          <el-table-column label="购买数量">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.equNum }}</span>
            </template>
          </el-table-column>
          <!-- 预计使用年限 -->
          <el-table-column label="预计使用年限">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.useYear }}</span>
            </template>
          </el-table-column>

          <!-- 购买时间 -->
          <el-table-column label="购买时间">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              <span style="margin-left: 10px">{{ scope.row.equTime }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
              ></el-link>
              <el-link
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)"
              ></el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 5, 10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>

      <!-- 添加模态窗体 -->
      <div>
        <el-dialog title="添加" :visible.sync="dialogFormVisible">
          <el-form :model="add">
            <el-form-item label="所属小区" :label-width="formLabelWidth">
              <el-select v-model="add.community" style="width: 730px">
                <el-option
                  v-for="name in names"
                  :key="name.id"
                  :label="name.comName"
                  :value="name.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- 设备编号 -->
            <el-form-item label="设备编号" :label-width="formLabelWidth">
              <el-input v-model="add.equId" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 设备名称 -->
            <el-form-item label="设备名称" :label-width="formLabelWidth">
              <el-input v-model="add.equName" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 品牌 -->
            <el-form-item label="品牌" :label-width="formLabelWidth">
              <el-input v-model="add.brand" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 购买单价 -->
            <el-form-item label="购买单价" :label-width="formLabelWidth">
              <el-input v-model="add.equPrice" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 购买数量 -->
            <el-form-item label="购买数量" :label-width="formLabelWidth">
              <el-input v-model="add.equNum" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 使用年限 -->
            <el-form-item label="使用年限" :label-width="formLabelWidth">
              <el-input v-model="add.useYear" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 设备购买时间 -->
            <el-form-item label="设备购买时间">
              <el-date-picker
                v-model="add.equTime"
                type="date"
                placeholder="请选择日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button :plain="true" type="primary" @click="addAdmin"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
      <!-- 编辑模态窗体 -->
      <div>
        <el-dialog title="修改" :visible.sync="dialogTableVisible">
          <!-- 所属小区 -->
          <el-form :model="form">
            <el-form-item label="所属小区" :label-width="formLabelWidth">
              <el-select v-model="form.community" style="width: 730px">
                <el-option
                  v-for="name in names"
                  :key="name.id"
                  :label="name.comName"
                  :value="name.comName"
                ></el-option>
              </el-select>
            </el-form-item>

            <!-- 设备编号 -->
            <el-form-item label="设备编号" :label-width="formLabelWidth">
              <el-input v-model="form.equId" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 设备名称 -->
            <el-form-item label="设备名称" :label-width="formLabelWidth">
              <el-input v-model="form.equName" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 品牌 -->
            <el-form-item label="品牌" :label-width="formLabelWidth">
              <el-input v-model="form.brand" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 购买单价 -->
            <el-form-item label="购买单价" :label-width="formLabelWidth">
              <el-input v-model="form.equPrice" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 购买数量 -->
            <el-form-item label="购买数量" :label-width="formLabelWidth">
              <el-input v-model="form.equNum" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 使用年限 -->
            <el-form-item label="使用年限" :label-width="formLabelWidth">
              <el-input v-model="form.useYear" autocomplete="off"></el-input>
            </el-form-item>
            <!-- 设备购买时间 -->
            <el-form-item label="设备购买时间">
              <el-date-picker
                v-model="form.equTime"
                type="datetime"
                placeholder="请选择日期"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
              ></el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button :plain="true" type="primary" @click="update"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      dialogTableVisible: false,
      formLabelWidth: "120px",
      currentPage: 1, //初始页
      pagesize: 5, //每页的数据
      names: "", //定义的一个空数组
      dialogImageUrl: "",
      dialogVisible: false,
      multipleSelection: [], //定义一个数组,用于复选框的选择
      //  checked: false,
      add: {
        community: "",
        equId: "",
        equName: "",
        brand: "",
        equPrice: 1200,
        equNum: 12,
        equTime: "",
        useYear: 12,
      },
      //搜索框

      startTime: "",
      endTime: "",
      equName: "",

      //修改
      form: {
        id: 1,
        community: "",
        equId: "",
        equName: "",
        brand: "",
        equPrice: 1200,
        equNum: 12,
        equTime: "",
        useYear: 12,
      },
      //页面数据展示
      tableData: [
        {
          id: 1,
          community: "状元壹号",
          equId: "xxx24464xx",
          equName: "大疆无人机",
          brand: "大疆",
          equPrice: 1200,
          equNum: 12,
          equTime: "2016-05-02",
          useYear: 12,
        },
        {
          id: 2,
          community: "状元贰号",
          equId: "xxx24464xx",
          equName: "大疆无人机",
          brand: "大疆",
          equPrice: 1200,
          equNum: 12,
          equTime: "2016-05-02",
          useYear: 12,
        },
        {
          id: 3,
          community: "状元叁号",
          equId: "xxx24464xx",
          equName: "大疆无人机",
          brand: "大疆",
          equPrice: 1200,
          equNum: 12,
          equTime: "2016-05-02",
          useYear: 12,
        },
      ],
    };
  },
  //面包屑导航栏

  //展示页面数据
  created: function () {
    // alert("找到了用户");
    //触发小区事件,查找到所有小区
    this.getAdmin();
    //展示页面数据事件
    this.$axios
      .get("assets/findAll")
      .then((res) => {
        if (res.data.code == 200) {
          this.tableData.length = 0;
          this.tableData = res.data.data;
        }
      })
      .catch((error) => {
        console.log(error);
      });
  },
  methods: {
    //页面刷新
    push: function () {
      location.reload();
    },
    //复选框删除
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },

    //搜索框
    onSubmit: function () {
      //console.log('submit!');
      //alert("xxxxxxx");
      this.$axios
        .post("/assets/findLike", {
          startTime: this.startTime,
          endTime: this.endTime,
          equName: this.equName,
        })
        .then((res) => {
          //alert("查询成功了");

          if (res.data.code == 200) {
            this.tableData = res.data.data;
            console.log(this.tableData);
            this.$message(res.data.msg);
          } else {
            this.$message(res.data.msg);
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //分页
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize); //每页下拉显示数据
    },
    handleCurrentChange: function (currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage); //点击第几页
    },
    //编辑
    handleEdit(index, row) {
      // alert(row.name+row.address);
      //alert(row.id);
      //显示模态窗体
      this.dialogTableVisible = true;
      this.form.id = row.id;
      this.form.community = row.community.comName;
      this.form.equId = row.equId;
      this.form.equName = row.equName;
      this.form.brand = row.brand;
      this.form.equPrice = row.equPrice;
      this.form.equNum = row.equNum;
      this.form.useYear = row.useYear;
      this.form.equTime = row.equTime;
      // this.form=row;
    },
    //遍历小区
    getAdmin: function () {
      //alert("进来了吗");
      this.$axios
        .get("/comm/findComm")
        .then((res) => {
          if (res.data.code == 200) {
            this.names = res.data.data; //在空数组追加相应的数据
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //修改
    update: function () {
      //alert(this.form.id+"----------------"+this.form.equTime+"-----------"+this.form.community);
      this.$axios
        .put("/assets/updateAll", {
          id: this.form.id,
          comName: this.form.community,
          equId: this.form.equId,
          equName: this.form.equName,
          brand: this.form.brand,
          equPrice: this.form.equPrice,
          equNum: this.form.equNum,
          useYear: this.form.useYear,
          equTime: this.form.equTime,
        })
        .then((res) => {
          //alert(res.data.code);
          if (res.data.code == 200) {
            alert("修改成功");
            this.$options.methods.push();
          } else {
            this.$message(res.data.msg);
          }
        })
        .catch((error) => {});
      this.dialogTableVisible = false;
    },

    //添加
    addA() {
      //显示添加按钮模态窗体内容
      this.dialogFormVisible = true;
    },
    addAdmin: function () {
      //alert("开始添加了");
      alert(this.add.community);
      this.$axios
        .post("/assets/addAll", {
          communityId: this.add.community,
          equId: this.add.equId,
          equName: this.add.equName,
          brand: this.add.brand,
          equPrice: this.add.equPrice,
          equNum: this.add.equNum,
          useYear: this.add.useYear,
          equTime: this.add.equTime,
        })
        .then((res) => {
          if ((res.data.code = 200)) {
            //alert("添加成功了");
            this.$message(res.data.msg);
            this.dialogFormVisible = false;
            this.$options.methods.push(); //在添加方法中调用刷新的方法
          } else {
            this.dialogFormVisible = false;
            this.$message(res.data.msg);
          }
        })
        .catch((error) => {});
      this.dialogTableVisible = false;
    },
    //删除
    handleDelete(index, row) {
      //console.log(index, row);
      if (confirm("确定要删除吗?")) {
        this.$axios
          .delete("/assets/deleteOne", {
            data: { id: row.id },
          })
          .then((res) => {
            if (res.data.code == 200) {
              this.tableData.splice(index, 1);
            }
          })
          .catch((error) => {});
      }
    },
    //选择删除
    deleteChoose: function (index, row) {
      //定义一个ids
      let ids = "";
      if (this.multipleSelection.length == 0) {
        alert("至少选择一行");
      } else {
        for (let j = 0; j < this.multipleSelection.length; j++) {
          ids += this.multipleSelection[j].id + ",";
        }
        //alert(ids);
        this.$axios
          .delete("/assets/deleteChoose", {
            params: {
              ids: ids,
            },
          })
          .then((res) => {
            // alert(res.data.code);
            if (res.data.code == 200) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              for (let j = 0; j < this.multipleSelection.length; j++) {
                //alert("for循环有用吗");
                this.tableData.splice(index, 1);
              }
            } else {
              this.$message({
                message: "删除失败",
                type: "error",
              });
            }
          })
          .catch((error) => {
            console.log(error);
          });
      }
    },
    //其他
  },
};
</script>
<style scoped>
#div1 {
  height: 30px;
  line-height: 30px;
}
#navigation {
  /* margin-top: 5px; */
  float: left;
}
#push {
  line-height: 20px;
  float: right;
  font-size: 20px;
}
#serch {
  text-align: center;
  line-height: 30px;
  font-size: 19px;
  margin-top: 30px;
}
#div3 {
  line-height: 50px;
}
#butt1 {
  float: left;
}
#butt2 {
  float: right;
}
#tableg {
  line-height: 30px;
  align-content: center;
}
#tableg .el-table__header th,
#tableg .el-table__header tr {
  background-color: blue;
}
</style>